import QtQuick 1.0

Rectangle {
	id: mainwindow
	width: 500
	height: 500
	Rectangle {
		id: table
		anchors.fill: parent
		gradient: Gradient {
			GradientStop {
			position: 0.0; color: "green"
			}
			GradientStop {
			position: 0.5; color: "yellow"
			}
			GradientStop {
			position: 1.0; color: "blue"
			}
		}

		opacity: 0.8
		Rectangle {
			id: bottomline
			width: 500
			height: 20
			anchors.baseline: parent.bottom
			anchors.baselineOffset: -20
			color:  "red"
		}
		Rectangle {
			id: nextline
			width:  bottomline.width/2
			height:  20
			anchors.baseline: bottomline.top
			anchors.baselineOffset: -200
			color:  "red"

		}
		Rectangle {
			id: player;
			width: 20; height: 20; radius:10
			anchors.baseline: bottomline.top
			anchors.baselineOffset: -height
			property int startx
			property int starty
			onXChanged: {
				if (jump.running==false)
				{
					if ( (player.anchors.baselineOffset==-200)
						|| (player.anchors.baselineOffset==0) )
					{
						fall.stop();
					}
					else
					{
						fall.start();
					}
				}

			}
			onYChanged:
			{
				if (jump.running==false)
				{
					if ( (player.anchors.baselineOffset==-200)
						|| (player.anchors.baselineOffset==0) )
					{
						fall.stop();
					}
					else
					{
						fall.start();
					}
				}
			}
		}

		PropertyAnimation{
			from: -200; to: 0
			duration: 10000
			target: bottomline
			properties:  "anchors.baselineOffset"
			running: false
			loops: Animation.Infinite
		}

	}
	PropertyAnimation{
		id: jump
		from: player.starty; to: player.starty-200
		duration: 400
		target: player
		properties:  "anchors.baselineOffset"
		easing.type: Easing.OutQuad
	}
	PropertyAnimation{
		id: fall
		from: player.anchors.baselineOffset; to: 0
		duration: 400
		target: player
		properties:  "anchors.baselineOffset"
		easing.type: Easing.InQuad
	}
	PropertyAnimation{
		id: goright;
		target: player;
		properties: "x";
		from: player.x; to:table.width-player.width;
		duration: 2000*(1-(player.startx/table.width));
	}
	PropertyAnimation{
		id: goleft;
		target: player;
		properties: "x";
		from: player.x; to:0;
		duration: 2000*(player.startx/table.width);
	}

	focus: true
	Keys.onPressed:{
		 if (event.key == Qt.Key_Left) {
			if (goleft.running==false)
			{
				goright.stop();
				 player.startx=player.x;
				 goleft.start();
			}
		 }
		 if (event.key == Qt.Key_Right) {
			if (goright.running==false)
			{
				goleft.stop();
				 player.startx=player.x
				 goright.start();
			}
		 }
		 if (event.key == Qt.Key_Space)
		 {
			 player.starty=player.anchors.baselineOffset
			 jump.start();

		 }
	}
	Keys.onReleased:{
		goright.stop();
		goleft.stop();
	}
}

